#!/usr/bin/python3
# -*- coding: utf-8 -*-
'模塊注釋'
import csv
import os
import time

from wushi.utils import dbutil
from wushi.utils.dbutil import connectDB

__author__ = 'Grant Wu'

def load_brand(file_name, fields, connect_db):
    sql = '''
    insert into mmd_db.bs_spider_brand(name,brand_id,platform,channel,thumb_nail,product_list_url,status,created_time,updated_time)
    values(%s,%s,%s,%s,%s,%s,%s,%s,%s)
    '''
    sq2 = '''
        select * from mmd_db.bs_spider_brand where product_list_url = '{}'
        '''

    with open(file_name, "r", encoding="utf-8") as f:
        csv_reader = csv.DictReader(f,fieldnames=fields)
        datas=[]
        data_detail=[]
        for row in csv_reader:
            if row.get('detail') and (not data_detail.__contains__(row.get('detail'))):
                data_detail.append(row.get('detail'))
                # 去重
                sq2 = sq2.format(row.get('detail'))
                old_data = dbutil.exeSelectSql(sq2, connect_db)
                if old_data:
                    connect_db
                datas.append((row.get('name'),None,'vip.com','女装',row.get('thumb_nail'),row.get('detail'),0,time.time()*1000,time.time()*1000))
        dbutil.exemanySql(datas,sql,connect_db)

def load_product(file_name, fields, connect_db):
    sql = '''
    insert ignore into mmd_db.bs_spider_product(product_id,title,channel,sale_price,sale_price_origin,thumnail_img_link,details_link,
    brand_id,brand_name,brand_details_link,platform,imgs_link,color,properties,status,created_time,updated_time)
    values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
    '''
    try:
        with open(file_name, "r", encoding="utf-8") as f:
            csv_reader = csv.DictReader(f,fieldnames=fields)
            datas=[]
            data_detail=[]
            for row in csv_reader:
                # 去重
                if not data_detail.__contains__(row.get('product_id')):
                    data_detail.append(row.get('product_id'))
                    datas.append((row.get('product_id'),row.get('title'),'女装',row.get('sale_price'),row.get('sale_price_origin'),row.get('thumnail_img_link'),row.get('details_link'),row.get('brand_id'),None,row.get('brand_details_link'),'vip.com',row.get('imgs_link'),None,row.get('properties'),0,time.time()*1000,time.time()*1000))
            dbutil.exemanySql(datas,sql,connect_db)
    except Exception as err:
        print('======>load file error %s, err: %s' % file_name,err)


if __name__ == '__main__':
    # load brands
    connect_db = connectDB('mmd_db', '192.168.6.76', 'root', 'root')
    brand_fields=['name','discount','thumb_nail','detail']
    brand_path = '''D:\\MyDoc\\Personal\\mmd\\vip-men-brand.csv'''
    brand_women_path='''D:\\MyDoc\\Personal\\mmd\\vip-women-brand.csv'''
    load_brand(brand_women_path,brand_fields,connect_db)


    # load products
    # product_fields = ['product_id', 'title', 'sale_price', 'sale_price_origin','thumnail_img_link','details_link','brand_id','brand_details_link','imgs_link','properties']
    #
    # start_path='''D:\\MyDoc\\Personal\\mmd\\vip-men-brand.csv'''
    # start_path_women='D:\MyDoc\Personal\mmd\women.tar'
    # if os.path.isdir(start_path_women):
    #     for file in os.listdir(start_path_women):
    #         print('=====>loading file %s' % file)
    #         absolute_file = os.path.abspath(os.path.join(start_path_women, file))
    #         load_product(absolute_file, product_fields,connect_db)